package com.zmn.oms.third.guomei.service.impl;

import com.alibaba.fastjson.JSON;
import com.zmn.base.common.data.common.dro.area.AreaDRO;
import com.zmn.base.common.data.dubbo.interfaces.area.AreaListRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.math.MoneyUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.baidumap.BaiduMapBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.bo.order.OrderBO;
import com.zmn.oms.model.dto.baidumap.LngLatLocation;
import com.zmn.oms.model.dto.order.proxy.CancelOrderDTO;
import com.zmn.oms.model.dto.order.proxy.CreateOrderDTO;
import com.zmn.oms.model.dto.order.proxy.OrderLogDTO;
import com.zmn.oms.model.dto.order.proxy.UpdateOrderDTO;
import com.zmn.oms.model.dto.product.OrderProductDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.ZsOrderWorkVO;
import com.zmn.oms.third.base.util.AESUtil;
import com.zmn.oms.third.guomei.GmqxConsts;
import com.zmn.oms.third.guomei.GmqxUtil;
import com.zmn.oms.third.guomei.dto.GmqxResultDTO;
import com.zmn.oms.third.guomei.dto.GmqxSyncRequestDTO;
import com.zmn.oms.third.guomei.dto.api.*;
import com.zmn.oms.third.guomei.service.GmqxService;
import com.zmn.oms.zmn.business.interfaces.proxy.ProxyOrderBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkQueryBService;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import com.zmn.pay.common.constant.PayConsts;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 类描述：与国美美云保清洗的对接接口实现
 *
 * @author XieWenBing
 * @date 2019/12/02 18:00
 */

@Service
@Slf4j
public class GmqxServiceImpl implements GmqxService {

    @Autowired
    private ProxyOrderBService proxyOrderBService;
    @Autowired
    private BaiduMapBService baiduMapBService;
    @Autowired
    protected ZsOrderWorkQueryBService zsOrderWorkQueryBService;
    @Autowired
    private WorkFlowContextBService workFlowContextBService;

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    protected AreaListRemoteService areaListRemoteService;

    /**
     * 处理国美美云保清洗发送的订单消息（验证服务区域，创建订单，取消订单等）
     * @param requestDTO
     * @return GmqxResultDTO
     * @author xiewenbing
     */
    public GmqxResultDTO handleSyncOrderMsg(GmqxSyncRequestDTO requestDTO) throws OmsBaseException {
        if (Objects.isNull(requestDTO)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR,"传入的数据不正确");
        }

        if (!Objects.equals(requestDTO.getAppId(),GmqxConsts.APP_ID)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"传入的appid不正确");
        }

        if (!GmqxUtil.verifyRequestSign(requestDTO)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"sign验证不通过");
        }

        String decryptData = String.valueOf(AESUtil.decrypt(requestDTO.getData(),GmqxConsts.AES_KEY));
        if (StringUtil.isNullOrEmpty(decryptData)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"数据解密失败");
        }

        requestDTO.setData(decryptData);

        log.info("[{}] 收到新的请求，请求数据:[{}]",GmqxConsts.CHANNEL_NAME,requestDTO);

        GmqxResultDTO resultDTO = new GmqxResultDTO();
        GmqxSyncResult syncResult = null;

        switch (requestDTO.getMethod()) {
            case GmqxConsts.METHOD_VERTIFY_AREA:
                syncResult = this.parseVertifyServiceAreaRequest(requestDTO);
                break;
            case GmqxConsts.METHOD_CREATE_ORDER:
                syncResult = this.parseCreateOrderRequest(requestDTO);
                break;
            case GmqxConsts.METHOD_VERTIFY_CANCEL_ORDER:
                break;
            case GmqxConsts.METHOD_CANCEL_ORDER:
                syncResult = this.parseCancelOrderRequest(requestDTO);
                break;
            case GmqxConsts.METHOD_UPDATE_ORDER:
                syncResult = this.parseUpdateOrderRequest(requestDTO);
                break;
            default:
                syncResult = new GmqxSyncResult();
                syncResult.setCode(GmqxConsts.RESPONSE_CODE_SEVER_ERROR);
                syncResult.setMsg("不支持的接口");
        }

        resultDTO.setResult(syncResult);
        return resultDTO;
    }

    /**
     * 生成缺省成功的返回信息结果
     * @author xiewenbing
     */
    private GmqxSyncResult getDefaultSyncResult() {
        GmqxSyncResult syncResult = new GmqxSyncResult();
        syncResult.setCode(GmqxConsts.RESPONSE_CODE_SUCCESS);
        syncResult.setMsg("success");
        return syncResult;
    }

    /**
     * 处理国美美云保发送的验证服务区域以及产品的请求
     * @param requestDTO
     * @author xiewenbing
     */
    public GmqxSyncResult parseVertifyServiceAreaRequest(GmqxSyncRequestDTO requestDTO) throws OmsBaseException {

        String requestBody = requestDTO.getData();
        if (Objects.isNull(requestBody) || requestBody.isEmpty()) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"传入的数据不正确");
        }

        GmqxVertifyServiceAreaParam vertifyServiceAreaParam = JSON.parseObject(requestBody,GmqxVertifyServiceAreaParam.class);
        if (Objects.isNull(vertifyServiceAreaParam)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR,"数据转换异常");
        }

        // 验证商品类型是否为清洗
        if (vertifyServiceAreaParam.getGoodsType() != GmqxConsts.SUPPORT_PRODUCT_TYPE_WASH) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_VERTIFY_ORDER_PRODUCT_NO_EXIST,"不支持的商品类型");
        }

        /*  暂时不验证 -- 2019.12.06
        // 验证商品是否存在 Xiewenbing
        ResponseDTO<ServProductDRO> responseServProductDTO = servProductRemoteService.findByKey(Integer.valueOf(vertifyServiceAreaParam.getGoodsId()));
        if (Objects.isNull(responseServProductDTO) || Objects.isNull(responseServProductDTO.getData())) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_VERTIFY_ORDER_PRODUCT_NO_EXIST,"不支持的商品类型");
        }

        // 验证区域是否支持 Xiewenbing
        com.zmn.baidumap.dto2.ResponseDTO<com.zmn.plat.baidumap.dto.area.AreaDRO> responseAreaDTO = areaListRemoteService.getByCode(Integer.valueOf(vertifyServiceAreaParam.getCountyCode()));
        if (Objects.isNull(responseAreaDTO) || Objects.isNull(responseAreaDTO.getData())) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_VERTIFY_AREA_UNSUPPORT,"不支持的区域");
        }
        */
        return getDefaultSyncResult();
    }

    /**
     * 处理国美美云保发送的创建订单的请求
     * @param requestDTO
     * @author xiewenbing
     */
    public GmqxSyncResult parseCreateOrderRequest(GmqxSyncRequestDTO requestDTO) throws OmsBaseException {

        // 参数判断
        String requestBody = requestDTO.getData();
        if (Objects.isNull(requestBody) || requestBody.isEmpty()) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"传入的数据不正确");
        }

        // JSON数据转换
        GmqxCreateOrderParam createOrderParam = JSON.parseObject(requestBody,GmqxCreateOrderParam.class);
        if (Objects.isNull(createOrderParam)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR,"数据转换异常");
        }

        String outerId = createOrderParam.getOrderId();
        // 判断订单号
        if (StringUtil.isNullOrEmpty(outerId)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"传入的数据不正确 - 没有订单号");
        }

        // 判断订单是否重复
        ZsOrderWorkVO orderWorkVO = zsOrderWorkQueryBService.findOrderWorkByOuterId(outerId, OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);
        if (Objects.nonNull(orderWorkVO) ) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"订单号重复，订单已经存在");
        }

        // 判断联系人电话
        if (StringUtil.isNullOrEmpty(createOrderParam.getUserPhone())) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"传入的数据不正确 - 没有用户电话");
        }

        // 判断产品信息
        if (Objects.isNull(createOrderParam.getItems()) || createOrderParam.getItems().size() <=0 ||
                Objects.isNull(createOrderParam.getItems().get(0).getGoodsId())) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"传入的数据不正确 - 商品信息不正确");
        }

        // 生成创建订单的数据
        CreateOrderDTO createOrderDTO = this.makeZmnCreateOrderData(createOrderParam);
        OrderBO orderBO = null;

        try {
            orderBO = proxyOrderBService.createOrder(createOrderDTO);
            log.info("【{}】保存订单成功 OrderId:[{}]", GmqxConsts.CHANNEL_NAME, orderBO.getOrder().getOrderId());
        } catch (Exception exception){
            log.error("【{}】保存订单失败 消息:[{}]", GmqxConsts.CHANNEL_NAME, exception.getMessage());
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR,"创建订单失败");
        }

        // 工单自动流转
        OrderWork orderWork = orderBO.getOrderWork();
        workFlowContextBService.asyncAutoFlow(orderWork.getOrderId(),orderWork.getWorkId());

        // 生成返回结果
        GmqxCreateOrderResult createOrderResult = new GmqxCreateOrderResult();
        createOrderResult.setCode(GmqxConsts.RESPONSE_CODE_SUCCESS);
        createOrderResult.setMsg("success");

        GmqxSyncCreateOrderItem createOrderItem = new GmqxSyncCreateOrderItem();
        createOrderItem.setGjOrderId(String.valueOf(orderWork.getOrderId()));

        List<GmqxSyncCreateOrderItem> orderItems = new LinkedList<>();
        orderItems.add(createOrderItem);
        createOrderResult.setItems(orderItems);

        return createOrderResult;
    }

    /**
     * 转换国美的订单信息为啄木鸟平台的创建订单数据对象
     * @param createOrderParam
     * @return CreateOrderDTO
     * @auth xieWenBing
     */
    protected CreateOrderDTO makeZmnCreateOrderData(GmqxCreateOrderParam createOrderParam) {
        CreateOrderDTO createOrderDTO = new CreateOrderDTO();

        // 设置外部数据
        GmqxOrderOutData outerData = BeanMapper.map(createOrderParam,GmqxOrderOutData.class);
        createOrderDTO.setOuterId(createOrderParam.getOrderId());

        // 将同步数据中的订单关键信息记录进日志中
        GmqxCreateOrderProductItem orderProductItem = createOrderParam.getItems().get(0);
        String guestMessage = Optional.ofNullable(createOrderParam.getMessage()).orElse("");

        String operatorLogRemark = String.format(
                "美云保订单号：%s，下单时间：%s，订单类型：%d，美云保子订单号：%s，商品Id：%s，结算价：%.2f，客户地址：%s，客户留言：%s",
                createOrderParam.getOrderId(), DateUtil.toString(createOrderParam.getOrderTime()),
                createOrderParam.getOrderType(), orderProductItem.getOrderSubId(), orderProductItem.getGoodsId(),
                Optional.ofNullable(orderProductItem.getSettlementPrice()).orElse(0d),
                createOrderParam.getAddress(), guestMessage);

        createOrderDTO.setOperatorLogRemark(operatorLogRemark);

        // 渠道id
        createOrderDTO.setChannelId(OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);

        // 联系人
        createOrderDTO.setContactName(createOrderParam.getUserName());

        // 电话
        createOrderDTO.setTelephone(createOrderParam.getUserPhone());

        // 地址
        createOrderDTO.setAddress(createOrderParam.getAddress());

        // 街道 - 暂时为空
        createOrderDTO.setStreet("");

        // 备注
        createOrderDTO.setRemark(guestMessage);

        // 平台标志
        createOrderDTO.setPlat(GlobalConsts.PLAT_MARK_ZMN);

        // 性别
        createOrderDTO.setGender(GlobalDict.GENDER_NO);

        // 是否允许弃单
        createOrderDTO.setDisableDiscard(GlobalConsts.NO);

        // 操作者
        createOrderDTO.setOperator("系统");

        // 操作者id
        createOrderDTO.setOperatorId((long) GlobalConsts.OPERATE_USER_TYPE_SYSTEM);

        // 操作者类型
        createOrderDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_CHANNEL);

        // 工单录入方式
        createOrderDTO.setInputType(OrderConsts.ORDER_INPUT_TYPE_API);

        // 城市id , 默认重庆
        Integer cityId = 500100;
        do {
            String areaName = createOrderParam.getPccName();
            if (StringUtil.isNullOrEmpty(areaName)) {
                break;
            }

            // 重新设置一下具体地址
            createOrderDTO.setAddress(areaName+createOrderDTO.getAddress());
            LngLatLocation areaLocation = baiduMapBService.location(areaName);
            if (Objects.isNull(areaLocation)) {
                break;
            }
            ResponseDTO<AreaDRO> areaResponse = areaListRemoteService.getAreaByLatAndLng(Double.valueOf(areaLocation.getLng()), Double.valueOf(areaLocation.getLat()));
            if (Objects.isNull(areaResponse) || Objects.isNull(areaResponse.getData())) {
                break;
            }

            cityId = Integer.valueOf(areaResponse.getData().getParentId());

            // 获取经纬度 Xiewenbing 2019.12.19
            Double longitude = Double.valueOf(areaResponse.getData().getLongitude());
            Double latitude = Double.valueOf(areaResponse.getData().getLatitude());
            createOrderDTO.setLongitude(longitude);
            createOrderDTO.setLatitude(latitude);
        } while (false);

        createOrderDTO.setCityId(cityId);

        // 预约时间
        if (Objects.nonNull(orderProductItem.getAppointmentInfo())) {
            Date appointmentDate = orderProductItem.getAppointmentInfo().getAppointmentDate();
            String startTime = orderProductItem.getAppointmentInfo().getStartTime();

            if (Objects.nonNull(appointmentDate)){

                //判断是否有开始时间
                if (!StringUtil.isNullOrEmpty(startTime)) {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
                    String dutyTime = formatter.format(appointmentDate) + startTime;

                    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    ParsePosition pos = new ParsePosition(0);
                    createOrderDTO.setDutyTime(formatter.parse(dutyTime, pos));
                } else {
                    createOrderDTO.setDutyTime(appointmentDate);
                }
            }
        }

        // 产品信息
        OrderProductDTO orderProduct = new OrderProductDTO();
        orderProduct.setProductId(Integer.valueOf(orderProductItem.getGoodsId()));
        Integer productNum = Double.valueOf(orderProductItem.getNum()).intValue();
        orderProduct.setNumber( productNum > 0 ? productNum : 1);

        // 兼容之前价格逻辑  by lhl 20.04.27
        // 04.27晚上发版，但国美的结算价要晚一天才会上线（04.28），临时做一下兼容：没有结算价前，仍使用老的价格作为渠道预付款
        if (Objects.nonNull(createOrderParam.getPayment())) {
            Integer payMoney = NumberUtil.convertMoneyToFen(createOrderParam.getPayment().getPayMoney());
            if (NumberUtil.isNotNullOrZero(payMoney)) {
                createOrderDTO.setChannelPrepayAmount(payMoney);
                createOrderDTO.setChannelPrepayStatus(PayConsts.PAY_STATUS_DONE);
            }
        }

        // 结算价格作为渠道预付款保存
        if (Objects.nonNull(orderProductItem.getSettlementPrice())) {
            Integer payMoney = NumberUtil.convertMoneyToFen(orderProductItem.getSettlementPrice());
            if (NumberUtil.isNotNullOrZero(payMoney)) {
                createOrderDTO.setChannelPrepayAmount(payMoney * orderProduct.getNumber());
                createOrderDTO.setChannelPrepayStatus(PayConsts.PAY_STATUS_DONE);
            }
        }

        // 添加增加对办公室消毒服务的日志处理 xiewenbing 2020.03.04
        if (orderProduct.getProductId() == GmqxConsts.ORDER_PRODUCT_OFFICE_DISINFECT_ID) {
            Integer areaNum = Double.valueOf(orderProductItem.getArea()).intValue();
            operatorLogRemark = createOrderDTO.getOperatorLogRemark();
            operatorLogRemark += String.format(",平米数:%d ",areaNum);
            createOrderDTO.setOperatorLogRemark(operatorLogRemark);
        }

        outerData.setOrderSubId(orderProductItem.getOrderSubId());
        createOrderDTO.setOuterData(JSON.toJSONString(outerData));

        List<OrderProductDTO> list = new LinkedList<>();
        list.add(orderProduct);
        createOrderDTO.setOrderProductList(list);

        log.info("【{}-API】 创建的的订单信息:[{}]",GmqxConsts.CHANNEL_NAME,createOrderDTO.toString());

        return createOrderDTO;
    }

    /**
     * 处理国美美云保发送的取消订单的请求
     * @param requestDTO
     * @author xiewenbing
     */
    public GmqxSyncResult parseCancelOrderRequest(GmqxSyncRequestDTO requestDTO) throws OmsBaseException {

        // 参数判断
        String requestBody = requestDTO.getData();
        if (Objects.isNull(requestBody) || requestBody.isEmpty()) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"传入的数据不正确");
        }

        // JSON数据转换
        GmqxCancelOrderParam cancelOrderParam = JSON.parseObject(requestBody,GmqxCancelOrderParam.class);
        if (Objects.isNull(cancelOrderParam)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR,"数据转换异常");
        }

        // 查找对应的订单
        String outerId;
        ZsOrderWorkVO orderWorkVO;
        do {
            outerId = cancelOrderParam.getOrderId();
            orderWorkVO = zsOrderWorkQueryBService.findOrderWorkByOuterId(outerId, OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);

            if (Objects.nonNull(orderWorkVO)) {
                break;
            }

            outerId = cancelOrderParam.getOrderSubId();
            orderWorkVO = zsOrderWorkQueryBService.findOrderWorkByOuterId(outerId, OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);
            if (Objects.nonNull(orderWorkVO)) {
                break;
            }

            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR, "找不到对应的订单");
        }while(false);

        log.info("【{}】取消订单：订单Id:[{}]，OuterId:[{}]，取消原因:[{}]，取消时间:[{}]", GmqxConsts.CHANNEL_NAME,
                orderWorkVO.getOrderId(), outerId, cancelOrderParam.getReason(),cancelOrderParam.getCannelTime());

        // 取消订单
        CancelOrderDTO cancelOrderDTO = new CancelOrderDTO();
        cancelOrderDTO.setOrderId(orderWorkVO.getOrderId());
        cancelOrderDTO.setOuterId(outerId);
        cancelOrderDTO.setChannelId(OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);
        cancelOrderDTO.setOperatorId((long)OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);
        cancelOrderDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_CHANNEL);
        cancelOrderDTO.setRemark(cancelOrderParam.getReason());

        try {
            proxyOrderBService.cancelOrder(cancelOrderDTO);
        } catch (OmsBaseException omsBaseException) {
            log.error("【{}】取消订单失败 消息:[{}]", GmqxConsts.CHANNEL_NAME, omsBaseException.getMessage());
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR,"取消订单失败");
        }

        return getDefaultSyncResult();
    }

    /**
     * 处理国美美云保发送的修改订单的请求
     * @param requestDTO
     * @author xiewenbing
     */
    public GmqxSyncResult parseUpdateOrderRequest(GmqxSyncRequestDTO requestDTO) throws OmsBaseException {

        // 参数判断
        String requestBody = requestDTO.getData();
        if (Objects.isNull(requestBody) || requestBody.isEmpty()) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_DATA_VERITFY_FAILED,"传入的数据不正确");
        }

        // JSON数据转换
        GmqxUpdateOrderParam updateOrderParam = JSON.parseObject(requestBody,GmqxUpdateOrderParam.class);
        if (Objects.isNull(updateOrderParam)) {
            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR,"数据转换异常");
        }

        // 查找对应的订单
        String outerId;
        ZsOrderWorkVO orderWorkVO;
        do {
            outerId = updateOrderParam.getOrderId();
            orderWorkVO = zsOrderWorkQueryBService.findOrderWorkByOuterId(outerId, OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);
            if (Objects.nonNull(orderWorkVO)) {
                break;
            }

            outerId = updateOrderParam.getOrderSubId();
            orderWorkVO = zsOrderWorkQueryBService.findOrderWorkByOuterId(outerId, OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);
            if (Objects.nonNull(orderWorkVO)) {
                break;
            }

            throw new OmsBaseException(GmqxConsts.RESPONSE_CODE_SEVER_ERROR, "找不到对应的订单");
        }while(false);

        Double settlementPrice = updateOrderParam.getSettlementPrice();

        log.info("【{}】修改订单：订单Id:[{}]，OuterId:[{}]，area:[{}]{}", GmqxConsts.CHANNEL_NAME,
                orderWorkVO.getOrderId(), outerId, updateOrderParam.getArea(),
                settlementPrice == null ? "" : String.format("，settlementPrice:[%.2f]", settlementPrice));

        if (settlementPrice != null) {
            try {
                UpdateOrderDTO updateOrderDTO = new UpdateOrderDTO();
                updateOrderDTO.setOrderId(orderWorkVO.getOrderId());
                updateOrderDTO.setWorkId(orderWorkVO.getWorkId());
                updateOrderDTO.setOuterId(outerId);
                updateOrderDTO.setChannelPrepayAmount(MoneyUtil.parseToFen(settlementPrice));

                proxyOrderBService.updateChannelPrepayAmount(updateOrderDTO);
            } catch (Exception e) {
                log.error(String.format("【{}】修改渠道预付款失败：{}", GmqxConsts.CHANNEL_NAME, e.getMessage()), e);

                GmqxSyncResult syncResult = new GmqxSyncResult();
                syncResult.setCode(GmqxConsts.RESPONSE_CODE_SEVER_ERROR);
                syncResult.setMsg("更改结算价格失败！");
                return syncResult;
            }
        }

        try {
            // 修改订单日志记录
            OrderLogDTO orderLogDTO = new OrderLogDTO();
            orderLogDTO.setOrderId(orderWorkVO.getOrderId());
            orderLogDTO.setOuterId(outerId);
            orderLogDTO.setChannelId(OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);
            orderLogDTO.setOperatorId((long)OrderConsts.CHANNEL_ID_GUOMEI_MYBQX);
            orderLogDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_CHANNEL);
            orderLogDTO.setType(OrderLogConsts.ORDER_LOG_TYPE_CHANNEL_REMARK);
            orderLogDTO.setOperatorLogRemark(String.format("服务面积修改：%.2f 平米%s", updateOrderParam.getArea(),
                    settlementPrice == null ? "" : String.format("，结算价修改：%.2f 元", settlementPrice)));

            proxyOrderBService.addOrderLog(orderLogDTO);
        } catch (OmsBaseException omsBaseException) {
            log.error("【{}】保存订单操作日志失败：{}", GmqxConsts.CHANNEL_NAME, omsBaseException.getMessage());
        }

        return getDefaultSyncResult();
    }
}
